package com.zc.im.netty.handler;

import com.zc.im.util.ImGolbalTypeContext;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.data.redis.core.RedisTemplate;

public class ChatServerInitializer extends ChannelInitializer<Channel> {

	private final ChannelGroup group;

	public ChatServerInitializer(ChannelGroup group) {
		super();
		this.group = group;
	}

	@Override
	protected void initChannel(Channel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();

		pipeline.addLast(new HttpServerCodec());
		
		pipeline.addLast(new ChunkedWriteHandler());
		
		pipeline.addLast(new HttpObjectAggregator(Integer.parseInt(ImGolbalTypeContext.propertiesMap.get(ImGolbalTypeContext.NETTY_MAXCONTENT_LENGTH))));
		
		pipeline.addLast(new HttpRequestHandler(ImGolbalTypeContext.propertiesMap.get(ImGolbalTypeContext.NETTY_WS_URI)));
		
		pipeline.addLast(new WebSocketServerProtocolHandler(ImGolbalTypeContext.propertiesMap.get(ImGolbalTypeContext.NETTY_WEB_SOCKET_PATH)));

		pipeline.addLast(new TextWebSocketFrameHandler(group));
		
	}

}
